import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class demo1 {
    int sum=0;
    int[]temp;
    int[]index;
    HashMap<Integer,Integer>map;
    ArrayList<Integer> list=new ArrayList<>();
    public List<Integer> countSmaller(int[] nums) {
        temp=new int[nums.length];//注意提前定义
        // index=new int[nums.length];
        map=new HashMap<>();
        gbin(nums,0,nums.length-1);
        list.add(0);
        for(int i=0;i<nums.length;i++){
            list.add(map.get(i));
        }
        return list;
    }
    public void gbin(int[]nums,int left,int right){
        int sum=0;
        if(left>=right)return ;
        int mid=(left+right)/2;
        //先将数组左右分组再进行合并
        gbin(nums,left,mid);
        gbin(nums,mid+1,right);
        int cur1=left,cur2=mid+1,x=0;
        while(cur1<=mid&&cur2<=right){
            //  temp[x++]=nums[cur1]<=nums[cur2] ? nums[cur1++] : nums[cur2++];
            if(nums[cur1]<=nums[cur2]){
                temp[x++]=nums[cur2++];
            }else{
                temp[x++]=nums[cur1];
                sum+=right-cur2+1;
                map.put(cur1,sum);
                cur1++;
            }
        }
        list.add(sum);
        while(cur1<=mid)temp[x++]=nums[cur1++];
        while(cur2<=right)temp[x++]=nums[cur2++];
        for(int i=left;i<=right;i++){
            nums[i]=temp[i-left];
        }

    }
}
